#include <hd_led.h>
#include <hd_oled.h>
#include <pd_spi_hd.h>
#include <hd_oled_protocol_spi.h>
#include <pd_spi_sim.h>
#include <hd_w25qxx.h>
#include <pd_i2c_hd.h>
#include <hd_oled_protocol_i2c.h>
#include <delay.h>
#include "main.h"


#define ARRAY_SIZE(x)       sizeof(x) / sizeof(x[0])
#define USE_HARDWARE_SPI    1
#define USE_HARDWARE_I2C    1
OLED oled;

extern void SystemClock_Config(void);

void oled_instance(OLED *oled);


int main(void) {

    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

    oled_instance(&oled);

    OLED_printf(&oled, 0, 00, "MID:    DID:");
    OLED_printf(&oled, 0, 16, "R:      W:");
    OLED_printf(&oled, 0, 32, "MID:    DID:");
    OLED_printf(&oled, 0, 48, "R:      W:");
    OLED_refresh(&oled);


    // SPI GPIO
    GPIO spi1SselGPIO[3], spi1SclkGPIO, spi1MisoGPIO, spi1MosiGPIO;
    GPIO_constructor(spi1SselGPIO + 0, GPIOA, LL_GPIO_PIN_2); // OLED
    GPIO_constructor(spi1SselGPIO + 1, GPIOA, LL_GPIO_PIN_4); // W25Q64 on chip
    GPIO_constructor(spi1SselGPIO + 2, GPIOC, LL_GPIO_PIN_4); // W25Q64 external
    GPIO_constructor(&spi1SclkGPIO, GPIOA, LL_GPIO_PIN_5);    // Clock, SPIx_SCK
    GPIO_constructor(&spi1MisoGPIO, GPIOA, LL_GPIO_PIN_6);    // MISO,  SPIx_MISO
    GPIO_constructor(&spi1MosiGPIO, GPIOA, LL_GPIO_PIN_7);    // MOSI,  SPIx_MOSI

#ifdef USE_HARDWARE_SPI
    // SPI hardware init
    SPIHardware spi1;
    SPIHardware_constructor(&spi1, SPI1, spi1SselGPIO, ARRAY_SIZE(spi1SselGPIO),
                            &spi1SclkGPIO, &spi1MosiGPIO, &spi1MisoGPIO);
#elif USE_SIMULATE_SPI
    // SPI simulate init
    SPISimulate spi1;
    SPISimulate_constructor(&spi1, spi1SselGPIO, ARRAY_SIZE(spi1SselGPIO),
                            &spi1SclkGPIO, &spi1MosiGPIO, &spi1MisoGPIO);
#endif
    SPI_init((SPI *) &spi1);

    // Storage of w25q64 init
    W25Qxx w25Qxx3OnChip;
    W25Qxx_constructor(&w25Qxx3OnChip, (SPI *) &spi1, &spi1SselGPIO[1]);
    W25Qxx_init(&w25Qxx3OnChip);

    uint8_t mID;
    uint16_t dID;
    W25Qxx_getId(&w25Qxx3OnChip, &mID, &dID);
    OLED_printf(&oled, 32, 0, "%02X", mID);
    OLED_printf(&oled, 96, 0, "%02X", dID);

    uint32_t COUNT = 2;
    uint8_t array[] = {0xBA, 0xBA,};
    const static uint32_t ADDR = 0x001000;

    while (1) {
        // write
        W25Qxx_write(&w25Qxx3OnChip, ADDR, array, COUNT);
        for (int i = 0; i < COUNT; ++i) {
            OLED_printf(&oled, 80 + i * 8 * 3, 16, "%02X", array[i]);
        }
        OLED_refresh(&oled);

        delay_ms(1000);
        // read
        W25Qxx_read(&w25Qxx3OnChip, ADDR, array, COUNT);
        for (int i = 0; i < COUNT; ++i) {
            OLED_printf(&oled, 16 + i * 8 * 3, 16, "%02X", array[i]);
        }
        OLED_refresh(&oled);

        for (int i = 0; i < COUNT; ++i) {
            array[i] += 1;
        }

        delay_ms(1000);
    }
}


void oled_instance(OLED *oled) {
    // I2C gpio init
    static GPIO oledSckGPIO, oledSdaGPIO;
    GPIO_constructor(&oledSckGPIO, GPIOB, LL_GPIO_PIN_10); // I2Cx_SCK
    GPIO_constructor(&oledSdaGPIO, GPIOB, LL_GPIO_PIN_11); // I2Cx_SDA

#ifdef USE_HARDWARE_I2C
    static I2CHardware i2c2;
    I2CHardware_constructor(&i2c2, I2C2, &oledSckGPIO, &oledSdaGPIO);
#elif USE_SIMULATE_I2C
    static I2CSimulate i2c2;
    I2CSimulate_constructor(&i2c2, &oledSckGPIO, &oledSdaGPIO);
#endif
    I2C_init((I2C *) &i2c2);

    // OLED protocol of I2C
    static OLEDProtocolI2C oledProtocolI2c;
    OLEDProtocolI2C_constructor(&oledProtocolI2c, (I2C *) &i2c2);

    // OLED font instance.
    static OLEDFont oledFont;
    OLEDFont_contructor(&oledFont, NULL);

    // OLED instance of 4-PIN(I2C)
    OLED_constructor(oled, (OLEDProtocol *) &oledProtocolI2c, &oledFont);

    OLED_init(oled);

    OLED_clear(oled);
    OLED_refresh(oled);
}
